clear all
set more off
cd "/Users/yasenov/Dropbox (IPL)/RefugeeBan/Data"

******************
***** PANEL A ****
******************

use "WRAPS/wraps_fips.dta"

keep if year >= 2010

merge 1:1 fips year using "crime/crime.dta", keep(1 3) keepusing(property violent)
keep if _merge == 3   // careful!!
drop _merge

merge 1:1 fips year using "demographics/demographics.dta", keep(1 3)
assert _merge == 3
drop _merge

gen statefip = substr(fips,1,2)

******************
******************

gen lproperty_crime = log(property_crime + 1)
gen rproperty_crime = (property_crime / pop_total) * 100000

gen lviolent_crime = log(violent_crime + 1)
gen rviolent_crime = (violent_crime / pop_total) * 100000

gen lrefugees = log(refugees + 1)
gen rrefugees = (refugee / pop_total) * 100

keep if year <= 2016

tabstat rproperty rviol lprop lviol, stats(p50 sem)

qreg rproperty, q(.5) vce(robust)
global RPROPERTY_P50 = _b[_cons]
qreg rviol, q(.5) vce(robust)
global RVIOLENT_P50 = _b[_cons]

qui sum rrefugees
global RREFUGEES_SD = r(sd)

qui sum lrefugees
global LREFUGEES_SD = r(sd)


********************
***** PANEL B.1 ****
********************

use "WRAPS/wraps_fips.dta", clear
keep if year >= 2015

merge 1:1 fips year using "crime/crime.dta", keep(1 3) keepusing(property violent)
drop if _merge == 1   
drop _merge

merge 1:1 fips year using "demographics/demographics.dta", keep(1 3)
drop if _merge == 1 
drop _merge

tab year
recode year (2015=2016) (2018=2017)
tab year

gen lproperty_crime = log(property_crime + 1)
gen rproperty_crime = (property_crime / pop_total) * 100000

gen lviolent_crime = log(violent_crime + 1)
gen rviolent_crime = (violent_crime / pop_total) * 100000

gen lrefugees = log(refugees + 1)
gen rrefugees = (refugee / pop_total) * 100

collapse (mean) lproperty rproperty rviolent lviolent rrefugees lrefugees pop_total, by(year fips)

sum

reshape wide lproperty rproperty rviolent lviolent rrefugees lrefugees pop_total, i(fips) j(year)

gen d_rproperty_crime = rproperty_crime2017 - rproperty_crime2016
gen d_rviolent_crime = rviolent_crime2017 - rviolent_crime2016
gen d_rrefugees = (rrefugees2017 - rrefugees2016) // careful!!

gen d_lproperty_crime = lproperty_crime2017 - lproperty_crime2016
gen d_lviolent_crime = lviolent_crime2017 - lviolent_crime2016
gen d_lrefugees = lrefugees2017 - lrefugees2016

gen county_fips = substr(fips, 3, 3)
gen state_fips = substr(fips, 1, 2)

**************
**************

global options cluster(state_fips) 
global weight 
global controls 

global if1 if abs(d_rproperty) <= 1000 
global if2 if abs(d_rviolent) <= 1000 
global if3 if abs(d_lproperty) <= 1 
global if4 if abs(d_lviolent) <= 1 

qui reg d_rproperty d_rrefugees $controls $if1 $weight, $options
lincom _b[d_rrefugees] * $RREFUGEES_SD

qui reg d_rviolent d_rrefugees $controls $if2 $weight, $options
lincom _b[d_rrefugees] * $RREFUGEES_SD

qui reg d_lproperty d_lrefugees $controls $if3 $weight, $options
lincom _b[d_lrefugees] * 1   // (in percent units)

qui reg d_lviolent d_lrefugees $controls $if4 $weight, $options
lincom _b[d_lrefugees] * 1  // (in percent units)




********************
***** PANEL B.2 ****
********************

use "WRAPS/wraps_fips.dta", clear
keep if year >= 2010

merge 1:1 fips year using "crime/crime.dta", keep(1 3) keepusing(property violent)
drop if _merge == 1 
drop _merge

merge 1:1 fips year using "demographics/demographics.dta", keep(1 3)
drop if _merge == 1   
drop _merge

gen lproperty_crime = log(property_crime + 1)
gen rproperty_crime = (property_crime / pop_total) * 100000

gen lviolent_crime = log(violent_crime + 1)
gen rviolent_crime = (violent_crime / pop_total) * 100000

* treatment vars for year 2016
gen lrefugees2016 = log(refugees + 1)
gen rrefugees2016 = (refugee / pop_total) * 100

sort fips year
bysort fips: replace lrefugees2016 = lrefugees2016[7]
bysort fips: replace rrefugees2016 = rrefugees2016[7]

destring fips, replace
destring fips_state, replace

**************
**************

gen dd = (year >= 2017) * rrefugees2016
gen dd_log = (year >= 2017) * lrefugees2016

**************
**************
**************

global options cluster(fips_state) 
global weight 
global trends i.fips#c.year
global controls 

global if1 
global if2 
global if3 
global if4 


qui reghdfe rproperty_crime dd $controls $if1 $weight, $options absorb(year fips)
lincom _b[dd] * (-1) * $RREFUGEES_SD

qui reghdfe rviolent_crime dd $controls $if2 $weight, $options absorb(year fips)
lincom _b[dd] * (-1) * $RREFUGEES_SD

qui reghdfe lproperty_crime dd_log $controls $if3 $weight, $options absorb(year fips)
lincom _b[dd_log] * (-1) * 1  // (in percent units)

qui reghdfe lviolent_crime dd_log $controls $if4 $weight, $options absorb(year fips)
lincom _b[dd_log] * (-1) * 1  // (in percent units)
